package com.wcl.demo.ui.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.wcl.demo.R;
import com.wcl.demo.model.entity.KeepHealthDaoEntity;
import com.wcl.demo.network.VolleySingleton;
import com.wcl.demo.utils.HanZiPinYinTranslate;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/12/5.
 */
public class SearchWeatherActivity extends BaseActivity implements View.OnClickListener {
    private TextView cityNameTv, weatherTv, tempTv, WDTv, WSTv, dataTv, start, I_tempTv, h_tempTv, linear_layout;
    private EditText cityNameEt;
    private LinearLayout activity_weather_back;

    @Override
    protected int getLayout() {
        return R.layout.activity_weather;
    }

    @Override
    protected void initView() {

        activity_weather_back = (LinearLayout) findViewById(R.id.activity_weather_back);

        cityNameTv = (TextView) findViewById(R.id.cityNameTv);
        weatherTv = (TextView) findViewById(R.id.weatherTv);
        tempTv = (TextView) findViewById(R.id.tempTv);
        WDTv = (TextView) findViewById(R.id.WDTv);
        WSTv = (TextView) findViewById(R.id.WSTv);
        dataTv = (TextView) findViewById(R.id.dataTv);
        start = (TextView) findViewById(R.id.start);
        start.setOnClickListener(this);
        h_tempTv = (TextView) findViewById(R.id.h_tempTv);
        I_tempTv = (TextView) findViewById(R.id.I_tempTv);
        cityNameEt = (EditText) findViewById(R.id.cityNameEt);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        InputMethodManager methodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        methodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        requestData();
    }

    private void requestData() {
        String cityName = cityNameEt.getText().toString();
        char city = cityName.charAt(0);
        String pinyin = "";

        if (city >= 0x4e00 && city <= 0x9fa5) {
            pinyin = HanZiPinYinTranslate.getFirstSpell(cityName);
        } else {
            pinyin = cityName;
        }
        String httpUrl = "http://apis.baidu.com/apistore/weatherservice/weather";
        String httpArg = "citypinyin=" + pinyin;
        String path = httpUrl + "?" + httpArg;

        final JsonObjectRequest request = new JsonObjectRequest(path, null,
                new Response.Listener<JSONObject>() {
                    Gson gson = new Gson();
                    List<KeepHealthDaoEntity> datas = new ArrayList<>();

                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null && response.has("retData")) {
                            JSONObject retData = response.optJSONObject("retData");
                            String str = retData.toString();
                            KeepHealthDaoEntity data = gson.fromJson(str, KeepHealthDaoEntity.class);
                            cityNameTv.setText(data.getCity());
                            weatherTv.setText(data.getWeather());
                            if (data.getWeather().equals("晴")) {
                                Resources resources = getResources();
                                Drawable btnDrawable = resources.getDrawable(R.mipmap.weather_sunning);
                                activity_weather_back.setBackgroundDrawable(btnDrawable);
                            } else if (data.getWeather().indexOf("雨") != -1) {
//                                Log.i("shaco",data.getWeather()+"<<<<<<<++++++++");
                                Resources resources = getResources();
                                Drawable btnDrawable = resources.getDrawable(R.mipmap.weather_rain2);
                                activity_weather_back.setBackgroundDrawable(btnDrawable);
                            }
                            else if (data.getWeather().indexOf("雪") != -1) {
                                Resources resources = getResources();
                                Drawable btnDrawable = resources.getDrawable(R.mipmap.weather_snow);
                                activity_weather_back.setBackgroundDrawable(btnDrawable);

                            } else if (data.getWeather().indexOf("云") != -1) {
                                Resources resources = getResources();
                                Drawable btnDrawable = resources.getDrawable(R.mipmap.weather_yun);
                                activity_weather_back.setBackgroundDrawable(btnDrawable);

                            }

                            tempTv.setText(data.getTemp());
                            WDTv.setText(data.getWD());
                            WSTv.setText(data.getWS());
                            dataTv.setText(data.getDate());
                            h_tempTv.setText(data.getH_tmp());
                            I_tempTv.setText(data.getL_tmp());
                            datas.add(data);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };
        VolleySingleton.getInstance().addRequest(request);
    }

}
